define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'eduadmin/enroll/index',
                    add_url: 'eduadmin/enroll/add',
                    edit_url: 'eduadmin/enroll/edit',
                    del_url: 'eduadmin/enroll/del',
                    multi_url: 'eduadmin/enroll/multi',
                    dragsort_url: '',
                    table: 'enroll',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                columns: [
                    [
                        {checkbox: true},
                        { field: 'id', title: __('Id') },
                        { field: 'orderid', title: __('Orderid')},
                        { field: 'doc_studen_tname', title: __('Studentname')},
                        {field: 'branch_names', title: __('Branch')},
                        {field: 'course_name', title: __('Courseid')},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', formatter: Controller.api.formatter.date},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Controller.api.formatter.date},
                        {field: 'classcount', title: __('classcount')},
                        {field: 'classprice', title: __('Classprice'), operate:'BETWEEN'},
                        {field: 'totalamount', title: __('Totalamount'), operate:'BETWEEN'},
                        {field: 'paymount', title: __('Paymount'), operate:'BETWEEN'},
                        {field: 'paystatus', title: __('Paystatus'), visible:false, searchList: {"\u5df2\u7f34\u8d39":__('\u5df2\u7f34\u8d39'),"\u672a\u7f34\u8d39":__('\u672a\u7f34\u8d39')}},
                        {field: 'paystatus_text', title: __('Paystatus'), operate:false},
                        { field: 'followteacher_name', title: __('Followteacher')},                     
                        {field: 'status', title: __('Status'), visible:false, searchList: {"\u5df2\u8f6c\u5b66":__('\u5df2\u8f6c\u5b66'),"\u5df2\u5173\u95ed":__('\u5df2\u5173\u95ed'),"\u5df2\u9000\u5b66":__('\u5df2\u9000\u5b66'),"\u672a\u5206\u73ed":__('\u672a\u5206\u73ed'),"\u5df2\u5206\u73ed":__('\u5df2\u5206\u73ed')}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //分班
        classesgroup: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'eduadmin/enroll/classesgroup?classid=' + Backend.api.query('classid') + '&callback=' + Backend.api.query('callback'),
                    table: 'enroll',
                }
            });

            var table = $("#table");

            var columns = [
                { checkbox: true },
                { field: 'id', title: __('Id') },
                { field: 'orderid', title: __('Orderid') },
                { field: 'doc_studen_tname', title: __('Studentname') },
                { field: 'branch_names', title: __('Branch') },
                { field: 'course_name', title: __('Courseid') },
                { field: 'starttime', title: __('Starttime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.date },
                { field: 'endtime', title: __('Endtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.date },
                { field: 'classcount', title: __('classcount') },
                { field: 'classprice', title: __('Classprice'), operate: 'BETWEEN' },
                { field: 'totalamount', title: __('Totalamount'), operate: 'BETWEEN' },
                { field: 'paymount', title: __('Paymount'), operate: 'BETWEEN' },
                { field: 'paystatus_text', title: __('Paystatus'), operate: false },
                { field: 'followteacher_name', title: __('Followteacher') },
                { field: 'status_text', title: __('Status'), operate: false },
                { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
            ]
            if (Backend.api.query('callback')) {
                columns.push({ field: 'operate', title: __('Operate'), events: {
                        'click .btn-chooseone': function (e, value, row, index) {
                            var callback = Backend.api.query('callback');
                            var classid = Backend.api.query('classid');
                            parent.window[callback](classid, row.id);
                        },
                    }, formatter: function () {
                        var callback = Backend.api.query('callback');
                        if (callback == 'delStudent') {
                            return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-trash"></i>删除</a>';
                        } else {
                            return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-plus"></i>添加</a>';
                        }
                    }
                });
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                columns: columns
            });
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var callback = Backend.api.query('callback');
                var id = Backend.api.query('element_id');
                var idArr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    idArr.push(j.Id);
                });
                if (idArr.length == 0) {
                    alert("至少选择一个地磅！");
                    return false;
                }
                parent.window[callback](id, idArr.join(","));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();

            //选择器的初始化
            initSelectPage();
        },
        edit: function () {
            Controller.api.bindevent();
         
            //选择器的初始化
            initSelectPage();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                date: function (value, row, index) {
                    return value ? Moment(parseInt(value) * 1000).format("YYYY-MM-DD") : __('None');
                }
            }
        }
    };

    //选择器的初始化
    function initSelectPage() {   
           //学校选择器的初始化
           var lastBranch = '';
           require(['selectpage'], function () {
               $('#c-branchids', $("form[role=form]")).selectPage({
                  showField: 'name',
                  keyField: 'id',
                  data: 'branch/selectpage',
               eSelect: function (data) {
                       if(data && lastBranch !== data.id){
                           $('#c-courseid').selectPageClear();
                       }
               },
               eClear : function(){
                       $('#c-courseid').selectPageClear();
               }
               });
           });

           //课程选择器的初始化，使用空数据进行初始化
           require(['selectpage'], function () {
                   $('#c-courseid', $("form[role=form]")).selectPage({
                       showField: 'name',
                       keyField: 'id',
                       data: 'eduadmin/Course/selectpage',
                       //向服务端提交的参数中，增加已选中的
                       //设置返回-1，意为通知服务端返回空列表，初始化时使用
                       params: function(){
                           var pv = $('#c-branchids').val();
                           return {'custom[branchids]': pv?pv:'-1'};
                       }
               });
           });
    }
    return Controller;
});